package com.example.mylibrarybase.net;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.mylibrarybase.Interface.MyCallBack;
import com.example.mylibrarybase.utils.LogUtils;
import com.example.mylibrarybase.utils.UtilsToast;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by 35876 于萌萌
 * 创建日期: 11:04 . 2016年10月12日
 * 描述:访问网络的包装类
 * <p/>
 * <p/>
 * 备注:
 */
public class netWork {
    public static  String serverURL = "http://app.beta.9ac.com.cn/";

    //支付请求接口地址
    public static String PAYMENT_REQUEST = serverURL + "page/order/pay";
    public static  String MyTomCatserverURL = "http://192.168.0.144:8080/";
  // public static String testUrl = "http://192.168.1.101:8080/v.json";
  // public static String testUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=";
   //public static String testUrl = "http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key=%E9%93%B6%E9%AD%82&bk_length=600";
   //public static String testUrl = "http://ip.taobao.com/service/getIpInfo.php?ip=63.223.108.42";
  public static String testUrl0 = MyTomCatserverURL+"oder0.txt";
  public static String testUrl1 = MyTomCatserverURL+"oder1.txt";
  public static String testUrl2 = MyTomCatserverURL+"oder2.txt";
  public static String testUrl3 = MyTomCatserverURL+"oder3.txt";
  public static String testUrl = MyTomCatserverURL+"oder.txt";
  public static String testUrl11 = MyTomCatserverURL+"1v.json";
  public static String testlist = MyTomCatserverURL+"list.txt";
  public static String test222 = MyTomCatserverURL+"222.txt";
    public static String LOGIN_URL = serverURL+"page/login/login";


    private static JSONObject sJsonObject;


    public  interface CallBack{
        void onSuccess( JSONObject jsonObject);
        void onError(String error);

    }

   public interface CallBackArray{
        void onSuccess(String s);
       void onError(String error);
    }
    public static void goPost(final Context context , String url, HashMap hashMap, String cacheKey, CacheMode cacheMode , final CallBack callBack){
       // String url ="http://192.168.1.101:8080/v.json";

        OkGo.post(url)     // 请求方式和请求url
                .tag(context)       // 请求的 tag, 主要用于取消对应的请求
                .params(hashMap)
                .cacheKey(cacheKey)            // 设置当前请求的缓存key,建议每个不同功能的请求设置一个
                .cacheMode(cacheMode)

                // 缓存模式，详细请看缓存介绍
                .execute(new MyCallBack() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        try {
                            LogUtils.i("onSuccess原始数据"+s);
                           sJsonObject = JSON.parseObject(s);
                           // if (null!= sJsonObject)
                            callBack.onSuccess(sJsonObject);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                            UtilsToast.showToast(context, "json出错");
                        }
                    }

                    @Override
                    public void onErrorMy(Call call, Response response, Exception e) {
                        try {
                            if (null!=response){
                                LogUtils.i("onError原始数据"+response.body().string());
                                callBack.onError(response.body().string());
                            }else{
                                callBack.onError("访问网络失败");
                            }


                        } catch (Exception e1) {
                            e1.printStackTrace();
                            UtilsToast.showToast(context, "未知错误");
                        }
                    }
                });

    }
    public static void goPostArray(final Context context , String url, HashMap hashMap, String cacheKey, CacheMode cacheMode , final CallBackArray callBackArray){
       // String url ="http://192.168.1.101:8080/v.json";

        OkGo.post(url)     // 请求方式和请求url
                .tag(context)       // 请求的 tag, 主要用于取消对应的请求
                .params(hashMap)
                .cacheKey(cacheKey)            // 设置当前请求的缓存key,建议每个不同功能的请求设置一个
                .cacheMode(cacheMode)

                // 缓存模式，详细请看缓存介绍
                .execute(new MyCallBack() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        try {
                            LogUtils.i("onSuccessJSONArray原始数据"+s);
                            //SONArray jsonArray = JSON.parseArray(s);
                            // if (null!= sJsonObject)
                            callBackArray.onSuccess(s);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                            UtilsToast.showToast(context, "json出错");
                        }
                    }

                    @Override
                    public void onErrorMy(Call call, Response response, Exception e) {
                        try {
                            if (null!=response){
                                LogUtils.i("onError原始数据"+response.body().string());
                                callBackArray.onError(response.body().string());
                            }else{
                                callBackArray.onError("访问网络失败");
                            }


                        } catch (Exception e1) {
                            e1.printStackTrace();
                            UtilsToast.showToast(context, "未知错误");
                        }
                    }
                });

    }
    public static void goGet(Context context , String url, String cacheKey, CacheMode cacheMode , final CallBack callBack){
       // String url ="http://192.168.1.101:8080/v.json";

        OkGo.post(url)     // 请求方式和请求url
                .tag(context)       // 请求的 tag, 主要用于取消对应的请求
                .cacheKey(cacheKey)            // 设置当前请求的缓存key,建议每个不同功能的请求设置一个
                .cacheMode(cacheMode)

                // 缓存模式，详细请看缓存介绍
                .execute(new MyCallBack() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtils.i("onSuccess原始数据"+s);
                        JSONObject jsonObject = JSON.parseObject(s);
                        callBack.onSuccess(jsonObject);
                    }

                    @Override
                    public void onErrorMy(Call call, Response response, Exception e) {
                        try {
                            LogUtils.i("onError原始数据"+response.body().string());
                            callBack.onError(response.body().string());
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                });

    }
}
